home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / utility / uwserver.zip / uwserver.tar / server / uw_env.c < prev    next >
C/C++ Source or Header  |  1991-01-25  |  2KB  |  88 lines

  1. /*
  2.  *    uw_env - environment manipulation
  3.  *
  4.  * Copyright 1985,1986 by John D. Bruner.  All rights reserved.  Permission to
  5.  * copy this program is given provided that the copy is not sold and that
  6.  * this copyright notice is included.
  7.  */
  8.  
  9. #define    MAXENV    128    /* maximum number of arguments in environment */
  10.  
  11. static char *earray[MAXENV+1];
  12.  
  13. env_set(env)
  14. char **env;
  15. {
  16.     register char **ep1, **ep2, *cp;
  17.     char **ep3;
  18.     extern char **environ;
  19.  
  20.  
  21.     /*
  22.      * Merge the set of environment strings in "env" into the
  23.      * environment.
  24.      */
  25.  
  26.     /*
  27.      * The first time through, copy the environment from its
  28.      * original location to the array "earray".  This makes it a
  29.      * little easier to change things.
  30.      */
  31.  
  32.     if (environ != earray) {
  33.         ep1=environ;
  34.         ep2=earray;
  35.         while(*ep1 && ep2 <= earray+MAXENV)
  36.             *ep2++ = *ep1++;
  37.         *ep2++ = (char *)0;
  38.         environ = earray;
  39.     }
  40.  
  41.  
  42.     /*
  43.      * If "env" is non-NULL, it points to a list of new items to
  44.      * be added to the environment.  These replace existing items
  45.      * with the same name.
  46.      */
  47.  
  48.     if (env) {
  49.         for (ep1=env; *ep1; ep1++) {
  50.             for (ep2=environ; *ep2; ep2++)
  51.                 if (!env_cmp(*ep1, *ep2))
  52.                     break;
  53.             if (ep2 < earray+MAXENV) {
  54.                 if (!*ep2)
  55.                     ep2[1] = (char *)0;
  56.                 *ep2 = *ep1;
  57.             }
  58.         }
  59.     }
  60.  
  61.  
  62.     /* Finally, use an insertion sort to put things in order. */
  63.  
  64.     for (ep1=environ+1; cp = *ep1; ep1++) {
  65.         for(ep2=environ; ep2 < ep1; ep2++)
  66.             if (env_cmp(*ep1, *ep2) < 0)
  67.                 break;
  68.         ep3 = ep2;
  69.         for(ep2=ep1; ep2 > ep3; ep2--)
  70.             ep2[0] = ep2[-1];
  71.         *ep2 = cp;
  72.     }
  73. }
  74.  
  75.  
  76. static
  77. env_cmp(e1, e2)
  78. register char *e1, *e2;
  79. {
  80.     register d;
  81.  
  82.     do {
  83.         if (d = *e1 - *e2++)
  84.             return(d);
  85.     } while (*e1 && *e1++ != '=');
  86.     return(0);
  87. }
  88.